﻿var ns = namespace("kaskelot.sp.ui");

ns.bemanninggruppdetails = function () {

	function getGrupp() {
		var id = getParameterByName('GruppId');

		$.ajax({
			url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientBemanningService.svc/GetGruppDetails",
			type: "GET",
			data: { id: id },
			processData: true,
			cache: false,
			success:
				function (data) {
					if (!data.GetGruppDetailsResult.HasError) {
						$("#title-container").html(
							data.GetGruppDetailsResult.Data.GruppNamn
						);
					}
				},
			error:
				function (xhr) {
					kaskelot.sp.ui.prompts.showErrorPrompt(xhr.responseText);
				}
		});
	}

	function getBemanning() {
		var id = getParameterByName('GruppId');

		$.ajax({
			url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientBemanningService.svc/GetBemanningGrupp",
			type: "GET",
			data: { id: id },
			processData: true,
			cache: false,
			success:
				function (data) {
					if (!data.GetBemanningGruppResult.HasError) {
						$("#grupp-details #bemanning-results tbody").html(
							$("#bemanning-template").render(data.GetBemanningGruppResult.Data)
						);
					}
					else {

					}
				},
			error:
				function (xhr) {
					kaskelot.sp.ui.prompts.showErrorPrompt(xhr.responseText);
				}
		});
	}

	function removePerson(id) {
		$.ajax({
			url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientBemanningService.svc/RemovePersonFromBemanningGrupp",
			type: "GET",
			data: { id: id },
			processData: true,
			cache: false,
			success:
				function (data) {
					if (!data.RemovePersonFromBemanningGruppResult.HasError) {
						getBemanning();
					}
				},
			error:
				function (xhr) {
					kaskelot.sp.ui.prompts.showErrorPrompt(xhr.responseText);
				}
		});
	}

	function removeBemanning(id) {
		$.ajax({
			url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientBemanningService.svc/DeleteBemanningGrupp",
			type: "GET",
			data: { id: id },
			processData: true,
			cache: false,
			success:
				function (data) {
					if (!data.DeleteBemanningGruppResult.HasError) {
						getBemanning();
					}
				},
			error:
				function (xhr) {
					kaskelot.sp.ui.prompts.showErrorPrompt(xhr.responseText);
				}
		});
	}

	function showCreateForm() {
		var id = getParameterByName('GruppId');

		var selector = "#bemanninggrupp-form";
		kaskelot.sp.ui.formutils.getAndShowForm({
			params: {
				formName: "BemanningGruppForm",
				idString: "",
				parentIdString: id
			},
			selector: selector,
			openFunction: function () {
				$(selector + " .chzn-select-deselect").chosen({ allow_single_deselect: true, disable_search: true });
				$(selector + " #Antal").spinner({ min: 1, max: 100 });
			},
			okFunction: function () {
				$(selector).parent().block();

				var prog = kaskelot.sp.ui.formutils.getObject(selector);
				var data = "{\"form\":" + JSON.stringify(prog) + "}";

				var method = "CreateBemanningGrupp";

				$.ajax({
					url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientBemanningService.svc/" + method,
					data: data,
					type: "POST",
					processData: false,
					contentType: "application/json",
					dataType: "json",
					success: function (resp) {
						if (resp[method + "Result"].HasError) {
							kaskelot.sp.ui.formutils.showError(selector, resp[method + "Result"].ErrorMessage);
						}
						else {
							$(selector).dialog("close");
							getBemanning();
						}
					},
					error: function (xhr) {
						kaskelot.sp.ui.formutils.showError(selector, xhr.responseText);
					}
				});
			}
		});
	}

	function toggleTackatJa(id) {
		$.ajax({
			url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientBemanningService.svc/ToggleBemanningGruppTackatJa",
			type: "GET",
			data: { id: id },
			processData: true,
			cache: false,
			success:
				function (data) {
					if (!data.ToggleBemanningGruppTackatJaResult.HasError) {
						getBemanning();
					}
				},
			error:
				function (xhr) {
					kaskelot.sp.ui.prompts.showErrorPrompt(xhr.responseText);
				}
		});
	}

	return {
		getGrupp: getGrupp,
		getBemanning: getBemanning,
		removePerson: removePerson,
		removeBemanning: removeBemanning,
		showCreateForm: showCreateForm,
		toggleTackatJa: toggleTackatJa
	}
}();